//
//  GKPageScrollViewTestViewController.m
//  TestPrj
//
//  Created by 朱铅峰 on 2019/12/26.
//  Copyright © 2019 Xing. All rights reserved.
//

#import "GKPageScrollViewTestViewController.h"
#import <GKPageScrollView/GKPageScrollView.h>
#import <VTMagic/VTMagic.h>
#import <Masonry.h>
#import "ZFInTableViewController.h"

@interface GKPageScrollViewTestViewController ()<GKPageScrollViewDelegate,VTMagicViewDataSource,VTMagicViewDelegate>

proStrong UIView *headerView;
proStrong UIView *pageView;
proStrong GKPageScrollView *pageScrollView;
proStrong VTMagicController *magicController;

proStrong NSArray *titles;
proStrong NSArray *childVCs;

@end

@implementation GKPageScrollViewTestViewController

- (void)loadView {
    
    [super loadView];
    
    self.title = @"GKPageScrollView测试";
    self.navigationController.navigationBarHidden = YES;
    
    [self.view addSubview:self.pageScrollView];
    [self.pageScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self.pageScrollView reloadData];
    [self.magicController.magicView reloadData];
}

#pragma mark - GKPageScrollViewDelegate
- (UIView *)headerViewInPageScrollView:(GKPageScrollView *)pageScrollView {
    return self.headerView;
}

- (UIView *)pageViewInPageScrollView:(GKPageScrollView *)pageScrollView {
    return self.pageView;
}

- (NSArray<id<GKPageListViewDelegate>> *)listViewsInPageScrollView:(GKPageScrollView *)pageScrollView {
    return self.childVCs;
}

#pragma mark - VTMagicViewDataSource
- (NSArray<NSString *> *)menuTitlesForMagicView:(VTMagicView *)magicView {
    return self.titles;
}

- (UIButton *)magicView:(VTMagicView *)magicView menuItemAtIndex:(NSUInteger)itemIndex {
    static NSString *itemIdentifier = @"itemIdentifier";
    UIButton *menuItem = [magicView dequeueReusableItemWithIdentifier:itemIdentifier];
    if (!menuItem) {
        menuItem = [UIButton buttonWithType:UIButtonTypeCustom];
        [menuItem setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [menuItem setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
        menuItem.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:15.f];
    }
    return menuItem;
}

- (UIViewController *)magicView:(VTMagicView *)magicView viewControllerAtPage:(NSUInteger)pageIndex {
    return self.childVCs[pageIndex];
}

#pragma mark - VTMagicViewDelegate

//视图控制器显示到当前屏幕上时触发
- (void)magicView:(VTMagicView *)magicView viewDidAppear:(__kindof UIViewController *)viewController atPage:(NSUInteger)pageIndex {
    
    
}

//视图控制器从屏幕上消失时触发
- (void)magicView:(VTMagicView *)magicView viewDidDisappear:(__kindof UIViewController *)viewController atPage:(NSUInteger)pageIndex {
    
    
}

////选中导航菜单item时触发
//- (void)magicView:(VTMagicView *)magicView didSelectItemAtIndex:(NSUInteger)itemIndex;
//
////根据itemIndex获取对应menuItem的宽度，若返回结果为0，内部将自动计算其宽度
//- (CGFloat)magicView:(VTMagicView *)magicView itemWidthAtIndex:(NSUInteger)itemIndex;

////根据itemIndex获取对应slider的宽度，若返回结果为0，内部将自动计算其宽度
//- (CGFloat)magicView:(VTMagicView *)magicView sliderWidthAtIndex:(NSUInteger)itemIndex;


#pragma mark - 懒加载
- (GKPageScrollView *)pageScrollView {
    
    if (!_pageScrollView) {
        _pageScrollView = [[GKPageScrollView alloc] initWithDelegate:self];
        _pageScrollView.ceilPointHeight = kStatusBarHeight;//吸顶临界点高度（默认值：状态栏+导航栏）
        _pageScrollView.isAllowListRefresh = YES;
    }
    return _pageScrollView;
}

- (UIView *)headerView {
    
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, kStatusBarHeight+80)];
        
        UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _headerView.width, kStatusBarHeight)];
        statusBarView.backgroundColor = [UIColor greenColor];
        [_headerView addSubview:statusBarView];
        
        _headerView.backgroundColor = [UIColor orangeColor];
    }
    return _headerView;
}

- (UIView *)pageView {
    
    if (!_pageView) {
        _pageView = self.magicController.view;
    }
    return _pageView;
}

- (VTMagicController *)magicController {
    
    if (!_magicController) {
        _magicController = [[VTMagicController alloc] init];
        _magicController.magicView.navigationColor = [UIColor whiteColor];
        _magicController.magicView.sliderColor = [UIColor redColor];
        _magicController.magicView.layoutStyle = VTLayoutStyleDivide;
        _magicController.magicView.switchStyle = VTSwitchStyleDefault;
        _magicController.magicView.navigationHeight = 40.f;
        _magicController.magicView.dataSource = self;
        _magicController.magicView.delegate = self;
    }
    return _magicController;
}

- (NSArray *)titles {
    
    if (!_titles) {
        _titles = @[@"详情", @"热门", @"相关", @"聊天"];
    }
    return _titles;
}

- (NSArray *)childVCs {
    if (!_childVCs) {
        ZFInTableViewController *detailVC = [ZFInTableViewController new];
        ZFInTableViewController *hotVC = [ZFInTableViewController new];
        ZFInTableViewController *aboutVC = [ZFInTableViewController new];
        ZFInTableViewController *chatVC = [ZFInTableViewController new];
        _childVCs = @[detailVC, hotVC, aboutVC, chatVC];
    }
    return _childVCs;
}

@end
